package com.gaojinqi.sys.modular.rbac.controller;

import com.gaojinqi.base.common.exception.BizRuntimeException;
import com.gaojinqi.base.common.model.PageReq;
import com.gaojinqi.base.common.validation.group.Insert;
import com.gaojinqi.base.common.validation.group.Update;
import com.gaojinqi.base.common.vo.IResult;
import com.gaojinqi.base.common.vo.PageResultVo;
import com.gaojinqi.base.common.vo.ResultVo;
import com.gaojinqi.base.core.jpa.util.QueryGroupHelper;
import com.gaojinqi.sys.modular.rbac.model.Permission;
import com.gaojinqi.sys.modular.rbac.service.PermissionService;
import com.gaojinqi.sys.modular.rbac.vo.PermSaveVo;
import com.gaojinqi.sys.modular.rbac.vo.PermSearchVo;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.properties.StringProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.Collection;
import java.util.Objects;

/**
 * 权限 控制器
 *
 * @author gaojinqi
 * @version 1.0
 * @since 2020年05月19日
 */
@Api(tags = "权限")
@RestController
@Validated
@Slf4j
@RequestMapping(value = "/perm")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ApiOperation(value = "新增权限")
    @PostMapping
    public IResult<Permission> addPermission(@Validated(Insert.class) @RequestBody PermSaveVo param) {
        Permission permission = permissionService.findByPermission(param.getPermission());
        if (permission != null) {
            throw new BizRuntimeException("该权限编码已存在");
        }
        Permission entity = new Permission();
        BeanUtils.copyProperties(param, entity);
        if (entity.getSort() == null) {
            entity.setSort(0);
        }
        permissionService.save(entity);
        return ResultVo.suc(entity);
    }

    @ApiOperation(value = "修改权限")
    @PutMapping(value = "/{permissionId}")
    public IResult<Permission> updatePermission(
            @NotBlank(message = "请输入权限ID") @PathVariable(value = "permissionId") String permissionId,
            @Validated(Update.class) @RequestBody PermSaveVo param) {
        Permission permission = permissionService.findByPermission(param.getPermission());
        if (permission != null && !Objects.equals(permission.getPermissionId(), permissionId)) {
            throw new BizRuntimeException("该权限编码已存在");
        }
        Permission entity = new Permission();
        entity.setPermissionId(permissionId);
        BeanUtils.copyProperties(param, entity);
        permissionService.update(entity);
        return ResultVo.suc(entity);
    }

    @ApiOperation(value = "删除权限")
    @DeleteMapping(value = "/{permissionIds}")
    @ApiImplicitParam(name = "permissionIds", value = "权限ID list", allowMultiple = true, paramType = "path", dataType = StringProperty.TYPE, required = true)
    public IResult<String> delePermission(
            @NotEmpty(message = "请输入权限ID") @PathVariable(value = "permissionIds") String[] permissionIds) {
        permissionService.deleteByIds(Lists.newArrayList(permissionIds));
        return ResultVo.suc("删除成功");
    }

    @ApiOperation(value = "根据ID查询权限详情")
    @GetMapping(value = "/{permissionId}")
    public IResult<Permission> getPermission(
            @NotBlank(message = "请输入权限ID") @PathVariable(value = "permissionId") String permissionId) {
        return ResultVo.suc(permissionService.findById(permissionId));
    }

    @ApiOperation(value = "分页查询权限")
    @GetMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", defaultValue = "1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页条目数量", dataType = "int", defaultValue = "10", paramType = "query"),
            @ApiImplicitParam(name = "sortMode", value = "排序方式，倒序为0，正序为1", dataType = "int", defaultValue = "0", paramType = "query"),
    })
    public IResult<Collection<Permission>> page(PermSearchVo param, PageReq pageReq) {
        Permission entity = new Permission();
        BeanUtils.copyProperties(param, entity);
        return PageResultVo.suc(permissionService.page(entity, pageReq));
    }

    @ApiOperation(value = "查询所有权限")
    @GetMapping(value = "/all")
    public IResult<Collection<Permission>> findAll() {
        return ResultVo.suc(permissionService.findAll());
    }

}
